package com.jack.helloworld.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.jack.helloworld.R;
import com.jack.helloworld.activity.mode.BaseActivity;
import com.jack.helloworld.util.Constants;
import com.jack.helloworld.util.ToastUtil;
import com.orhanobut.logger.Logger;

import java.util.Arrays;
import java.util.List;

import androidx.annotation.NonNull;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

public class Main03Activity extends BaseActivity implements View.OnClickListener, EasyPermissions.PermissionCallbacks {

    Button btnDialog;
    Button btnOptionDialog;
    Button btnMultipleDialog;
    Button btnListDialog;
    Button btnCustomizeDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main03);

        initView();
        initData();
    }

    protected void initView() {
        btnDialog = (Button) findViewById(R.id.btnDialog);
        btnOptionDialog = (Button) findViewById(R.id.btnOptionDialog);
        btnMultipleDialog = (Button) findViewById(R.id.btnMultipleDialog);
        btnListDialog = (Button) findViewById(R.id.btnListDialog);
        btnCustomizeDialog = (Button) findViewById(R.id.btnCustomizeDialog);

    }


    protected void initData() {
        btnDialog.setOnClickListener(this);
        btnOptionDialog.setOnClickListener(this);
        btnMultipleDialog.setOnClickListener(this);
        btnListDialog.setOnClickListener(this);
        btnCustomizeDialog.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnDialog:
                showMyDialog();
                break;
            case R.id.btnOptionDialog:
                showMyOptionDialog();
                break;
            case R.id.btnMultipleDialog:
                showMyMultipleDialog();
                break;
            case R.id.btnListDialog:
                showMyListDialog();
                break;
            case R.id.btnCustomizeDialog:
                showMyCustomizeDialog();
                break;
        }
    }

    private void showMyDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setTitle("my dialog");
        builder.setMessage("show dialog");
        builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(Main03Activity.this, "click ok", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("cancle", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(Main03Activity.this, "click cancle", Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();

    }

    private void showMyOptionDialog() {
        final String[] sex = new String[]{"man", "woman", "other"};

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setTitle("my option dialog");
        builder.setSingleChoiceItems(sex, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(Main03Activity.this, "click " + sex[which], Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }

    private void showMyMultipleDialog() {
        final String[] hobby = new String[]{"NBA", "GAME", "music", "study"};

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setTitle("favourite");
        builder.setMultiChoiceItems(hobby, null, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                if (isChecked) {
                    Toast.makeText(Main03Activity.this, "select " + hobby[which], Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(Main03Activity.this, "cancel " + hobby[which], Toast.LENGTH_SHORT).show();
                }
            }
        });

        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }

    private void showMyListDialog() {
        final String[] hobby = new String[]{"NBA", "GAME", "music", "study"};

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setTitle("favourite");
        builder.setItems(hobby, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(Main03Activity.this, "select " + hobby[which], Toast.LENGTH_SHORT).show();
            }
        });


        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }

    private void showMyCustomizeDialog() {
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.view_loading, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setTitle("customize dialog");
        builder.setView(view);

        AlertDialog alertDialog = builder.create();
        alertDialog.show();

    }

    public void doClick(View view) {
        switch (view.getId()) {
            case R.id.btnEasyPermission:
                if (EasyPermissions.hasPermissions(this, Constants.PERMISSIONS_RW_EXTERNAL_STORAGE)) {
                    Logger.d("EasyPermission#hasPermissions");
                    ToastUtil.showShortToast(this, getString(R.string.get_rw_storage_permission));
                } else {
                    Logger.d("EasyPermission#requestPermissions");
                    EasyPermissions.requestPermissions(this, getString(R.string.request_rw_permission),
                            Constants.REQUEST_CODE_PERMISSIONS_RW_EXTERNAL_STORAGE,
                            Constants.PERMISSIONS_RW_EXTERNAL_STORAGE);
                }
                break;

            case R.id.btn_send_offline_broadcast:
                Intent intent = new Intent(Constants.FORCE_OFFLINE_BROADCAST);
                mContext.sendBroadcast(intent);
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        Logger.d("EasyPermission#onRequestPermissionsResult#grantResults: " + Arrays.toString(grantResults));
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        Logger.d("EasyPermission#onPermissionsGranted#perms: " + Arrays.toString(perms.toArray()));
        ToastUtil.showShortToast(this, getString(R.string.get_rw_storage_permission));
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
            new AppSettingsDialog.Builder(this).build().show();
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
        Logger.d("EasyPermission#onPermissionsDenied#perms: " + Arrays.toString(perms.toArray()));
        ToastUtil.showShortToast(this, getString(R.string.disable_rw_storage_permission));
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
            AppSettingsDialog dialog = new AppSettingsDialog.Builder(this).build();
            dialog.show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE) {
            Logger.d("EasyPermission#requestCode: " + requestCode + " ,resultCode: " + resultCode);
        }
    }
}
